package com.example.gbuffer.RenderPass;
import android.content.Context;
import android.opengl.GLES32;
import android.opengl.Matrix;

import com.example.gbuffer.Component.Model.CubeModel;
import com.example.gbuffer.Component.Framebuffer.Framebuffer;
import com.example.gbuffer.Component.Camera.PerspectiveCamera;
import com.example.gbuffer.Component.Shader;

public class GeoRenderPass extends RenderPass{

    private final static Shader m_geo_shader = new Shader();
    public static PerspectiveCamera m_camera_p = new PerspectiveCamera();
    public static CubeModel m_cube = new CubeModel();

    private static float[] m_modelMatrix = new float[16];

    public GeoRenderPass(Framebuffer buffer) {
        super(buffer);

        addGeoDrawCalls();
    }

    public void setPerspectiveCamera(PerspectiveCamera camera) {
        m_camera_p = camera;
    }

    public void initResources(Context ctx) {

        // 加载模型数据
        m_cube.load();

        // 生成并编译着色器
        m_geo_shader.setName("GeoShader");
        m_geo_shader.makeShaderProgramFromAssets(ctx, "geometry_pass.vs", "geometry_pass.fs");
    }

    private void addGeoDrawCalls() {
        super.addDrawCall(GeoRenderPass::drawCube);
    }

    private static Integer drawCube() {

        GLES32.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        GLES32.glClear(GLES32.GL_COLOR_BUFFER_BIT | GLES32.GL_DEPTH_BUFFER_BIT);
        GLES32.glEnable(GLES32.GL_DEPTH_TEST);

        m_geo_shader.use();

        /* red cube */
        // 设置模型矩阵
        Matrix.setIdentityM(m_modelMatrix, 0);
        Matrix.translateM(m_modelMatrix, 0, 0.0f, 0.0f, 0.0f);
        Matrix.rotateM(m_modelMatrix,0, 45.0f, 0.0f, 0.0f, 1.0f);
        Matrix.rotateM(m_modelMatrix,0, 30.0f, 0.0f, 1.0f, 0.0f);
        Matrix.scaleM(m_modelMatrix, 0, 2.0f, 2.0f, 2.0f);
        m_geo_shader.setUniformMatrix4fv("u_ModelMatrix", m_modelMatrix);

        // 设置视图矩阵
        m_geo_shader.setUniformMatrix4fv("u_ViewMatrix",
                m_camera_p.lookAt(0.0f,0.0f,10.0f,
                        0.0f,0.0f, 0.0f,
                        0,1,0));

        // 设置投影矩阵
        m_geo_shader.setUniformMatrix4fv("u_ProjMatrix",
                m_camera_p.project());

        // 设置物体颜色 uniform
        m_geo_shader.setUniform3fv("u_objectColor", new float[]{0.67f, 0.2f, 0.3f});

        m_cube.draw(m_geo_shader);

        /* green cube */
        // 设置模型矩阵
        Matrix.setIdentityM(m_modelMatrix, 0);
        Matrix.translateM(m_modelMatrix, 0, 3.6f, -2.7f, 1.3f);
        Matrix.rotateM(m_modelMatrix,0, 35.0f, 0.0f, 1.0f, 0.0f);
        Matrix.scaleM(m_modelMatrix, 0, 2.0f, 2.0f, 2.0f);
        m_geo_shader.setUniformMatrix4fv("u_ModelMatrix", m_modelMatrix);

        m_geo_shader.setUniform3fv("u_objectColor", new float[]{0.22f, 0.86f, 0.22f});

        m_cube.draw(m_geo_shader);

        /* blue cube */
        // 设置模型矩阵
        Matrix.setIdentityM(m_modelMatrix, 0);
        Matrix.translateM(m_modelMatrix, 0, 0f, -1.7f, -4.0f);
        Matrix.rotateM(m_modelMatrix,0, 67.0f, 0.0f, 0.0f, 1.0f);
        Matrix.scaleM(m_modelMatrix, 0, 2.0f, 2.0f, 2.0f);
        m_geo_shader.setUniformMatrix4fv("u_ModelMatrix", m_modelMatrix);

        m_geo_shader.setUniform3fv("u_objectColor", new float[]{0.22f, 0.22f, 0.87f});

        m_cube.draw(m_geo_shader);

        return 0;
    }

}
